<?php
class Con_surat_masuk extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library('auth');
		$this->load->model('suratmasukmodel');
		$this->load->model('usermodel');
	}
	public function list_surat_masuk($id=1)
	{
		
		$this->auth->restrict();
		$data['menu'] = $this->usermodel->get_menu();
		$jml_sm= $this->suratmasukmodel->get_all_surat_masuk();
		//start of pagination//
		$perpage = 5;
		$this->load->library('pagination');
		 $config = array(
            'base_url' => base_url() . 'index.php/con_surat_masuk/list_surat_masuk',
            'total_rows' => $jml_sm->num_rows(),
            'per_page' => $perpage,
			'first_page' => 'Awal',
			'last_page' => 'Akhir',
			'next_page' => '&laquo;',
			'prev_page' => '&raquo;'
        );
 
        //inisialisasi pagination dn config di atas
        $this->pagination->initialize($config);
        
		//buat pagination
		$data['halaman'] = $this->pagination->create_links();
		
		//tampilkan data
		$data['all_surat_masuk'] = $this->suratmasukmodel->get_surat_masuk($config['per_page'],$id);
		//end of pagination//
		
		//$data['all_surat_masuk'] = $this->suratmasukmodel->get_all_surat_masuk();
		
		$this->template->set('title','Daftar Surat Masuk | Aplikasi Persuratan');
		//$this->load->helper('tanggal');
		$this->template->load('template','list_surat_masuk',$data);
	}
	public function tambah()
	{
		$this->auth->restrict();
		$this->load->model('kantormodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('no_surat','no_surat','trim|required');
		$this->form_validation->set_rules('tanggal_surat','tanggal_surat','trim|required');
		$this->form_validation->set_rules('hal','hal','trim|required');
		$this->form_validation->set_rules('asal_surat','asal_surat','trim|required');
		$this->form_validation->set_rules('tanggal_diterima','tanggal_diterima','trim|required');
		$this->form_validation->set_rules('tanggapan','tanggapan','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = $this->usermodel->get_menu();
			$data['kantor'] = $this->kantormodel->get_all_kantor();
			$this->template->set('title','Tambah Surat Masuk | Aplikasi Persuratan');
			$this->template->load('template','form_tambah_surat_masuk',$data);
		}
		else
		{
			$data_surat_masuk = array (
			'id_user' =>$this->session->userdata('ID_USER'),
			'tanggal_diterima' =>$this->input->post('tanggal_diterima'),
			'no_surat' =>$this->input->post('no_surat'),
			'tanggal_surat' =>$this->input->post('tanggal_surat'),
			'hal' =>$this->input->post('hal'),
			'asal_surat' =>$this->input->post('asal_surat'),		
			'butuh_tanggapan' =>$this->input->post('tanggapan'),
			'disposisi1' =>$this->input->post('disposisi1'),
			'disposisi2' =>$this->input->post('disposisi2')
			);
			$this->suratmasukmodel->tambah_data_surat_masuk($data_surat_masuk);
			//kembali ke halaman list kantor
			redirect ('con_surat_masuk/list_surat_masuk');
		}
	}
	public function edit()
	{
		$this->auth->restrict();
		$this->load->model('kantormodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('no_surat','no_surat','trim|required');
		$this->form_validation->set_rules('tanggal_surat','tanggal_surat','trim|required');
		$this->form_validation->set_rules('hal','hal','trim|required');
		$this->form_validation->set_rules('asal_surat','asal_surat','trim|required');
		$this->form_validation->set_rules('tanggal_diterima','tanggal_diterima','trim|required');
		$this->form_validation->set_rules('tanggapan','tanggapan','trim|required');
		
		$this->form_validation->set_error_delimiters('<span style="color:#FF0000">','</span>');
		
		$id_surat_masuk = $this->uri->segment(3);
		
		if ($this->form_validation->run() == FALSE)
		{
			$data['menu'] = $this->usermodel->get_menu();
			$data['kantor'] = $this->kantormodel->get_all_kantor();
			$data['surat_masuk'] = $this->suratmasukmodel->get_surat_masuk_by_id($id_surat_masuk);
			$this->template->set('title','Edit Surat Masuk | Aplikasi Persuratan');
			$this->template->load('template','form_edit_surat_masuk',$data);
		}
		else
		{
			$data_surat_masuk = array (
			'id_user' =>$this->session->userdata('ID_USER'),
			'tanggal_diterima' =>$this->input->post('tanggal_diterima'),
			'no_surat' =>$this->input->post('no_surat'),
			'tanggal_surat' =>$this->input->post('tanggal_surat'),
			'hal' =>$this->input->post('hal'),
			'asal_surat' =>$this->input->post('asal_surat'),		
			'butuh_tanggapan' =>$this->input->post('tanggapan'),
			'disposisi1' =>$this->input->post('disposisi1'),
			'disposisi2' =>$this->input->post('disposisi2')
			);
			$this->suratmasukmodel->edit_data_surat_masuk($data_surat_masuk,$id_surat_masuk);
			
			//kembali ke halaman list surat masuk
			redirect ('con_surat_masuk/list_surat_masuk');
			//echo $data_surat_masuk->result();
		}
	}
	public function hapus()
	{
		//mencegah user yang belum login untuk mengakses halaman ini
		$this->auth->restrict();
		
		//dapatkan id user dari segment ke-3 dari URI
		$id = $this->uri->segment(3);
		$this->suratmasukmodel->delete_surat_masuk($id);
		//kembali ke halaman list surat masuk
		redirect ('con_surat_masuk/list_surat_masuk');
	}
		
}